
import { 
  _getTimeDate,
  _getTimeYear, 
  _getTimeMonth, 
  _getTimeDay,  
  DATE_YEAR, 
  DATE_MONTH, 
  DATE_DAY
} from '../../utils/DateFun.js';
Page({
  /**
   * 页面的初始数据
   * priceName       后台数据价格的字段名
   * stockName       后台数据库存的字段名
   * stateTypeName   后台数据库存状态的字段名
   * soldName        后台数据销量的字段名
   * startYear       日历遍历的起始年
   * startMonth      日历遍历的起始月
   * startDay        日历遍历的起始日
   * startDateStamp  日历遍历的起始日期的时间戳
   * checkDateObj    点击选中的日期对象
   * canChooseDate   设置日期是否可选中
   * hightLightDate  高亮日期
   * priceList       价格日历
   * currentYear     当前显示的年
   * currentMonth    当前显示的月
   * currentDay      当前显示的日
   * calendar        日历组件实例
   * ifShowPriceStock    判断是否需要显示库存价格的页面
   * showPrevBtn			判断是否显示切换上个月的箭头
   * calendarThemeColor  日历的主题色
    */
  data: {
    priceName: "marketPrice",
    stockName: "stockCount",
    stateTypeName: "state",
    soldName: "soldCount",
    startDateStamp: 0,
    startYear: 0,
    startMonth: 0,
    startDay: 0,
    checkDateObj: {},
    canChooseDate: false,
    hightLightDate: 0,
    priceList: [],
    currentYear: DATE_YEAR,
    currentMonth: DATE_MONTH,
    currentDay: DATE_DAY,
    chooseNum: 1,
    ifShowPriceStock: false,
    showPrevBtn: true,
    calendarThemeColor: "#F79727",
    priceList: [],
    priceTestList: [
			{
				copperPrice: 11,
				dateline: 1588118400,
				goldPrice: null,
				marketPrice: 111,
				packageId: 811,
				settlementPrice: 211,
				silverPrice: null,
				soldCount: 0,
				state: 0,
				stockCount: 200,
				stockId: 53918,
				updateTime: 1586398586,
				updater: 169,
				vipPrice: null
			},
			{
				copperPrice: 11,
				dateline: 1588204800,
				goldPrice: null,
				marketPrice: 111,
				packageId: 811,
				settlementPrice: 211,
				silverPrice: null,
				soldCount: 0,
				state: 0,
				stockCount: 200,
				stockId: 53918,
				updateTime: 1586398586,
				updater: 169,
				vipPrice: null
			},
			{
				copperPrice: 122,
				dateline: 1588291200,
				goldPrice: null,
				marketPrice: 133,
				packageId: 811,
				settlementPrice: 121,
				silverPrice: null,
				soldCount: 0,
				state: 0,
				stockCount: 200,
				stockId: 54118,
				updateTime: 1588066186,
				updater: 169,
				vipPrice: null
			},
			{
				copperPrice: 122,
				dateline: 1588377600,
				goldPrice: null,
				marketPrice: 133,
				packageId: 811,
				settlementPrice: 121,
				silverPrice: null,
				soldCount: 0,
				state: 0,
				stockCount: 200,
				stockId: 54118,
				updateTime: 1588066186,
				updater: 169,
				vipPrice: null
			},
			{
				copperPrice: 122,
				dateline: 1588464000,
				goldPrice: null,
				marketPrice: 133,
				packageId: 811,
				settlementPrice: 121,
				silverPrice: null,
				soldCount: 0,
				state: 0,
				stockCount: 200,
				stockId: 54118,
				updateTime: 1588066186,
				updater: 169,
				vipPrice: null
			},
			{
				copperPrice: 122,
				dateline: 1588550400,
				goldPrice: null,
				marketPrice: 133,
				packageId: 811,
				settlementPrice: 121,
				silverPrice: null,
				soldCount: 0,
				state: 0,
				stockCount: 200,
				stockId: 54118,
				updateTime: 1588066186,
				updater: 169,
				vipPrice: null
			},
			{
				copperPrice: 122,
				dateline: 1588636800,
				goldPrice: null,
				marketPrice: 133,
				packageId: 811,
				settlementPrice: 121,
				silverPrice: null,
				soldCount: 0,
				state: 0,
				stockCount: 200,
				stockId: 54118,
				updateTime: 1588066186,
				updater: 169,
				vipPrice: null
			}
    ],
    calendar:null
  },

  onLoad(options) {
    console.log(options)
    let startDateStamp = options.startDateStamp;
    if (options.hightLightDate !== 0) {
      //设定默认选中的日期对象
      let checkDateObj = {};
      checkDateObj.date = _getTimeDate(options.hightLightDate);
      checkDateObj.timeStamp = options.hightLightDate;
      wx.setStorageSync("chooseDate", checkDateObj);
      this.setData({
        //如果有选择（高亮）日期，那么当前日历遍历出的应显示高亮日期的年月日
        currentYear : _getTimeYear(options.hightLightDate),
        currentMonth : _getTimeMonth(options.hightLightDate),
        currentDay : _getTimeDay(options.hightLightDate),
        hightLightDate : options.hightLightDate,
        checkDateObj : checkDateObj
      })
    }
    console.log(startDateStamp)
    if (startDateStamp !== 0) {
      // 如果有规定起始日期，设置起始年，月，日
      this.setData({
        startYear : _getTimeYear(startDateStamp),
        startMonth : _getTimeMonth(startDateStamp),
        startDay : _getTimeDay(startDateStamp),
        startDateStamp : startDateStamp
      })
    }
    if ( this.data.currentYear == this.data.startYear && this.data.currentMonth <= this.data.startMonth ) {
      // 当前年月为规定的起始年和月，则切换不了上个月
      this.setData({
        showPrevBtn : false
      })
    }
    //从需要显示价格库存日历页面跳转，则日历组件需要调用获取价格库存数组的接口
    if (options.ifShowPriceStock == 'true') {
      this.setData({
        ifShowPriceStock : options.ifShowPriceStock
      })
      this.getPriceCalendarData();
    }
    // 获取日历组件的实例
    let calendar = this.selectComponent('#calendar');
    this.setData({
      calendar:calendar
    })
    //当数据处理后再调用初始化组件的方法
    calendar._handleInitialDateFun();
  },

  /**
  * @desc 请求价格日历接口，这里用假数据
  */
  getPriceCalendarData() {
    this.setData({
      priceList:this.data.priceTestList
    })
  },

 /**
  * @desc 切换上个月。全部时间的月份都是按0~11基准，显示月份才+1
  */
 lastMonthEvent() {
   if (this.data.showPrevBtn) {
     // 计算切换上个月的年
     let year =
       this.data.currentMonth - 1 < 1 ? this.data.currentYear - 1 : this.data.currentYear;
     // 计算切换上个月的月
     let month = this.data.currentMonth - 1 < 1 ? 12 : this.data.currentMonth - 1;
     // 计算切换上个月的日。如果切换后的年和月是当前电脑的时间，则日为起始遍历日历规定的日，否则为每个月的第一天(为了请求月头到月尾的价格库存的接口)
     let day = year == DATE_YEAR && month == DATE_MONTH ? this.data.startDay : 1;
     this.setData({
      currentYear : year,
      currentMonth : month,
      currentDay : day
     })
     if (this.data.ifShowPriceStock) {
       //需要显示库存和价格的日历，切换月份需重新请求获取价格日历对象
       this.getPriceCalendarData();
     }
     //调用组件里定义的切换上个月事件
     this.data.calendar._lastMonthEvent(year, month);
     if (
       year < this.startYear ||
       (year == this.data.startYear && month == this.data.startMonth)
     ) {
       // 如果切换后计算的年月小于起始遍历日历的年月，则不切换
       this.setData({
        showPrevBtn : false
       })
     }
   }
 },

 /**
  * @desc 切换下个月。全部时间的月份都是按0~11基准，显示月份才+1
  */
  nextMonthEvent() {
    this.setData({
      showPrevBtn : true
     })
    // 计算切换下个月的年
    let year = this.data.currentMonth + 1 > 12 ? this.data.currentYear + 1 : this.data.currentYear;
    // 计算切换下个月的月
    let month = this.data.currentMonth + 1 > 12 ? 1 : this.data.currentMonth + 1;
    // 计算切换下个月的日。如果下个月的年和月是当前电脑的时间，则日为当前电脑的日（起始日可能会遍历提前一个月的日），否则为每个月的第一天(为了请求月头到月尾的价格库存的接口)
    let day = year == DATE_YEAR && month == DATE_MONTH ? DATE_DAY : 1;
    this.setData({
      currentYear : year,
      currentMonth : month,
      currentDay : day
     })
    if (this.ifShowPriceStock) {
      //需要显示库存和价格的日历，切换月份需重新请求获取价格日历对象
      this.getPriceCalendarData();
    }
    //调用组件里定义的切换下个月事件
    this.data.calendar._nextMonthEvent(year, month);
  },

 /**
  * @desc 返回选中的日期对象
  * @property {string} date - 选中的带格式的日期
  * @property {string} timeStamp - 选中日期的时间戳
  * @property {string} unitPrice - 单价
  * @property {string} stock - 库存
  */
  getDate(e) {
    console.log(e)
    let checkDateObj = this.data.checkDateObj;
    if (checkDateObj.timeStamp) {
      checkDateObj.date = e.detail.day;
      checkDateObj.timeStamp = e.detail.timeStamp;
      checkDateObj.unitPrice = e.detail.price;
      checkDateObj.stock = e.detail.stock;
      this.setData({
        checkDateObj : checkDateObj
       })
      // 选中日期并存储日期对象
      wx.setStorageSync("chooseDate", checkDateObj);
    } 
    // 选择日期后回退上一页 delta(默认为1) 页面
    wx.navigateBack({
      delta: 1
    });
  }
})